/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.common.parsers.html;

import java.io.IOException;
import java.io.InputStream;

/**
 * A stream that combines two 'sources': first it provides bytes from an array
 * of bytes, then from the contained <b>stream</b>.
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 * &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2006/03/16 11:09:36 $
 */
class PrefilledInputStream extends InputStream {
    private InputStream stream;
    private byte[] prefilledBytes;
    private int passedBytesNumber;

    private PrefilledInputStream() {}

    /**
     * Constructor that takes a {@link RewindableInputStreamWrapper} instance
     * and gets <b>stream</b> and array of bytes from it.
     *
     * @param rewindableStreamWrapper stream
     * @see RewindableInputStreamWrapper
     */
    PrefilledInputStream(RewindableInputStreamWrapper rewindableStreamWrapper) {
        this.stream = rewindableStreamWrapper.getStream();
        this.prefilledBytes = rewindableStreamWrapper.getReadBytes();
        passedBytesNumber = 0;
    }

    /**
     * @see java.io.InputStream#available()
     */
    public int available() throws IOException {
        return prefilledBytes.length - passedBytesNumber + stream.available();
    }

    /**
     * @see java.io.InputStream#close()
     */
    public void close() throws IOException {
        prefilledBytes = new byte[0];
        passedBytesNumber = 0;
        stream.close();
    }

    /**
     * @see java.io.InputStream#reset()
     */
    public synchronized void reset() throws IOException {
        passedBytesNumber = 0;
        stream.reset();
    }

    /**
     * @see java.io.InputStream#markSupported()
     */
    public boolean markSupported() {
        return false;
    }

    /**
     * @see java.io.InputStream#mark(int)
     */
    public synchronized void mark(int readlimit) {
    }

    /**
     * @see java.io.InputStream#skip(long)
     */
    public long skip(long n) throws IOException {
        int prefilledBytesLeft = prefilledBytes.length - passedBytesNumber;
        long skippedByStream;
        long skippedAmongPrefilled;

        if (n <= prefilledBytesLeft) {
            passedBytesNumber += n;
            skippedByStream = 0;
            skippedAmongPrefilled = n;
        } else {
            passedBytesNumber = prefilledBytes.length;
            skippedByStream = stream.skip(n - prefilledBytesLeft);
            skippedAmongPrefilled = prefilledBytesLeft;
        }
        return skippedAmongPrefilled + skippedByStream;
    }

    /**
     * @see java.io.InputStream#read(byte[])
     */
    public int read(byte[] b) throws IOException {
        return super.read(b);
    }

    /**
     * @see java.io.InputStream#read(byte[], int, int)
     */
    public int read(byte[] b, int off, int len) throws IOException {
        return super.read(b, off, len);
    }

    /**
     * @see java.io.InputStream#read() 
     */
    public int read() throws IOException {
        if (passedBytesNumber < prefilledBytes.length) {
            return prefilledBytes[passedBytesNumber++];
        } else {
            return stream.read();
        }
    }
}
